package com.ksvip.next.components.oss;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.sts.model.v20150401.AssumeRoleResponse.Credentials;

/**
 * oss 连接
 * 
 * @author liks 2019年9月19日 下午5:28:52
 */
public class OssStsSpringProviderBean {
    private final Logger      logger      = LoggerFactory.getLogger(this.getClass());
    private final OssConfigVO ossConfigVO = new OssConfigVO();

    /**
     * 默认有效期半小时
     * 
     * @param objectname object文件名称
     * @param roleArn 需要授权的角色名称
     * @param policyEnum
     * @return
     */
    public OssConfigVO getSts(final String objectname, final String roleArn, final PolicyEnum policyEnum) {
        return this.getSts(objectname, roleArn, null, policyEnum);
    }

    /**
     * 获取链接
     * 
     * @param objectname object文件名称
     * @param roleArn 需要授权的角色名称
     * @param expirationTime 有效期
     * @param policyEnum
     * @return
     */
    public OssConfigVO getSts(final String objectname, final String roleArn, final Long expirationTime,
                              final PolicyEnum policyEnum) {
        try {
            Credentials credentials = null;
            if (expirationTime == null || expirationTime == 0L) {
                credentials = OssStsUtils.createSTS(this.ossConfigVO.getBucket(), objectname, roleArn,
                        this.ossConfigVO.getAccessKeyId(), this.ossConfigVO.getAccessKeySecret(), policyEnum,
                        this.ossConfigVO.getStsEndpoint());
            } else {
                credentials = OssStsUtils.createSTS(this.ossConfigVO.getBucket(), objectname, roleArn,
                        this.ossConfigVO.getAccessKeyId(), this.ossConfigVO.getAccessKeySecret(), expirationTime,
                        policyEnum, this.ossConfigVO.getStsEndpoint());
            }
            final OssConfigVO configVO = new OssConfigVO();
            configVO.setAccessKeyId(credentials.getAccessKeyId());
            configVO.setAccessKeySecret(credentials.getAccessKeySecret());
            configVO.setBucket(this.ossConfigVO.getBucket());
            configVO.setEndpoint(this.ossConfigVO.getEndpoint());
            configVO.setExpiration(credentials.getExpiration());
            configVO.setSecurityToken(credentials.getSecurityToken());
            return configVO;
        } catch (final ClientException e) {
            this.logger.error("", e);
            System.out.println(e);

        }
        return null;
    }

    public void setAccessKeyId(final String accessKeyId) {
        this.ossConfigVO.setAccessKeyId(accessKeyId);
    }

    public void setAccessKeySecret(final String accessKeySecret) {
        this.ossConfigVO.setAccessKeySecret(accessKeySecret);
    }

    public void setBucket(final String bucket) {
        this.ossConfigVO.setBucket(bucket);
    }

    public void setEndpoint(final String endpoint) {
        this.ossConfigVO.setEndpoint(endpoint);
    }

    public void setStsEndpoint(final String stsEndpoint) {
        this.ossConfigVO.setStsEndpoint(stsEndpoint);
    }

}
